<template>
  <div class="theme-all">
    <div class="solidColor">
      <span class="only">纯色</span>
      <div class="demo-all">
        <template v-for="(item, index) in themeColors" :key="item">
          <div
            class="demoColor"
            :class="[index === currentIndex ? 'activeColor' : '']"
            :style="{ background: item }"
            @click="handelClick(index)"
          ></div>
        </template>
      </div>
    </div>
    <div class="solidColor">
      <span class="custom">自定义颜色</span>
      <div>
        <el-color-picker
          v-model="color"
          show-alpha
          :predefine="predefineColors"
          @change="handelChange"
        />
      </div>
    </div>
    <div class="close">
      <el-button type="primary" round size="mini" @click="handelClose"
        >关闭</el-button
      >
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, ref, defineEmits } from 'vue'
import { useStore } from 'vuex'
import LocalCache from '@/utils/cache'
const store = useStore()
const currentIndex = ref(0)
currentIndex.value = LocalCache.getCache('defalutTheme')
const themeColors = computed(() => store.state.theme.defaultColors)
const color = ref('rgba(19, 206, 102, 0.8)')
const predefineColors = ref([
  '#ff4500',
  '#ff8c00',
  '#ffd700',
  '#90ee90',
  '#00ced1',
  '#1e90ff',
  '#c71585',
  'rgba(255, 69, 0, 0.68)',
  'rgb(255, 120, 0)',
  'hsv(51, 100, 98)',
  'hsva(120, 40, 94, 0.5)',
  'hsl(181, 100%, 37%)',
  'hsla(209, 100%, 56%, 0.73)',
  '#c7158577'
])
const handelClick = (index: number) => {
  currentIndex.value = index
  store.commit('theme/updateBcColor', index)
  LocalCache.setCache('defalutTheme', index)
  LocalCache.setCache('defalutColor', null)
}
const handelChange = (color: string) => {
  if (color) {
    store.commit('theme/updateColor', color)
    LocalCache.setCache('defalutColor', color)
    LocalCache.setCache('defalutTheme', 99)
    currentIndex.value = 99
  }
}
const emit = defineEmits(['handelClose'])
const handelClose = () => {
  emit('handelClose', false)
}
</script>

<style scoped lang="less">
.theme-all {
  width: 330px;
  height: 260px;
  border: 1px solid #eee;
  position: absolute;
  right: 50px;
  top: 80px;
  z-index: 99;
  background: #ffffff;
  padding: 5px 10px;
  .close {
    text-align: right;
  }
  .solidColor {
    width: 100%;
    padding: 6px 0;
    div {
      text-align: center;
    }
    span {
      color: #333;
      padding: 6px 5px;
    }
    span.only {
      border-bottom: 2px solid #ff5c8a;
    }
    span.custom {
      display: block;
    }
    .demo-all {
      width: 100%;
      display: flex;
      justify-content: space-evenly;
      flex-wrap: wrap;
      margin-top: 15px;
      .demoColor {
        width: 40px;
        height: 40px;
        border-radius: 5px;
        margin: 5px 6px;
        border: 1px solid #eee;
        cursor: pointer;
      }
      .activeColor {
        border: 1px solid red;
      }
    }
  }
}
</style>
